package com.sg.business.management.field.valuedelegator;

import org.bson.types.ObjectId;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.IMessageManager;

import com.mobnut.db.model.ModelService;
import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.DeliverableDefinition;
import com.sg.business.model.DocumentDefinition;
import com.sg.widgets.commons.valuevalidator.IFieldInputValidator;
import com.sg.widgets.registry.config.FieldConfigurator;

public class DocumentDefInputValidator implements
		IFieldInputValidator {

	@Override
	public boolean validate(PrimaryObject data,
			FieldConfigurator fieldConfigurator, Object valueForUpdate,
			IMessageManager messageManager, Control control) {
		if (valueForUpdate instanceof ObjectId) {
			ObjectId _id = (ObjectId) valueForUpdate;
			Object documentd_id = data
					.getValue(DeliverableDefinition.F_DOCUMENT_DEFINITION_ID);
			Object desc = data.getValue(DeliverableDefinition.F_DESC);
			if (_id != null && !_id.equals(documentd_id)) {
				if (desc == null || "".equals(desc)) {
					DocumentDefinition documentDef = ModelService
							.createModelObject(DocumentDefinition.class, _id);
					data.setValue(DeliverableDefinition.F_DESC,
							documentDef.getDescription(), null, true);
				}
			}
		}
		return true;
	}

}
